﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using libmorph;

namespace med
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            this.blurMaskComboBox.SelectedIndex = 0;
            this.operationMaskComboBox.SelectedIndex = 0;

            this.OnUpdate();

        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;

            System.Drawing.Bitmap bmp = null;

            try
            {
                bmp = new Bitmap(openFileDialog1.FileName);
            }
            catch (Exception)
            {
                MessageBox.Show("Błąd wczytywania bitmapy, prawdopodobnie wczytano błędny plik", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.m_source = bmp;
            this.m_sourceGray = libmorph.GrayscaleImage.CreateFromImage(bmp);
            this.pbIn.Image = this.m_source;
            this.pbGray.Image = this.m_sourceGray.toBitmap();

            this.OnUpdate();
        }

        private void OnUpdate()
        {
            this.toolStripProgressBar1.Value = 0;

            switch (this.blurMaskComboBox.SelectedIndex)
            {
                case 0:
                    this.m_blurMask = Mask.SquareMask((int)this.nudBlurMask.Value);
                    break;
                case 1:
                    this.m_blurMask = Mask.CircleMask((int)this.nudBlurMask.Value);
                    break;
                case 2:
                    this.m_blurMask = Mask.CrossMask((int)this.nudBlurMask.Value);
                    break;
            }

            switch (this.operationMaskComboBox.SelectedIndex)
            {
                case 0:
                    this.m_operationMask = Mask.SquareMask((int)this.nudOperationMask.Value);
                    break;
                case 1:
                    this.m_operationMask = Mask.CircleMask((int)this.nudOperationMask.Value);
                    break;
                case 2:
                    this.m_operationMask = Mask.CrossMask((int)this.nudOperationMask.Value);
                    break;
            }

            this.pbBlurMask.Image = this.m_blurMask.Clamp().toBitmap();
            this.pbOperationMask.Image = this.m_operationMask.Clamp().toBitmap();
            this.toolStripProgressBar1.Value = 1;

            if (this.m_sourceGray == null)
            {
                this.toolStripProgressBar1.Value = 7;
                return;
            }
            BlurMaskPictureBox.Visible = true;
            operationMaskPictureBox.Visible = true;
            SoucePictureBox.Visible = true;
            GrayPictureBox.Visible = true;
            blurPictureBox.Visible = true;
            resultPictureBox.Visible = true;
            dilationPictureBox.Visible = true;
            erodePictureBox.Visible = true;
            obminErPictureBox.Visible = true;
            dylminobPictureBox.Visible = true;

            this.m_blur = this.m_sourceGray.Blur(this.m_blurMask);
            this.pbBlur.Image = this.m_blur.Clamp().toBitmap();
            this.toolStripProgressBar1.Value = 2;
            
            this.m_dilated = this.m_blur.ModifiedDilation(m_operationMask) * 3.0f;
            this.pbDilation.Image = this.m_dilated.Clamp().toBitmap();
            this.toolStripProgressBar1.Value = 3;

            this.m_eroded = this.m_blur.ModifiedErode(m_operationMask) * 3.0f;
            this.pbErode.Image = this.m_eroded.Clamp().toBitmap();
            this.toolStripProgressBar1.Value = 4;

            this.m_ObrazMinErozja = (this.m_blur - this.m_eroded);
            this.pbobminer.Image = (this.m_ObrazMinErozja).Clamp().toBitmap();
            this.toolStripProgressBar1.Value = 5;

            this.m_DylatacjaMinObraz = (this.m_dilated - this.m_blur);
            this.pbdylminob.Image = (this.m_DylatacjaMinObraz).Clamp().toBitmap();
            this.toolStripProgressBar1.Value = 6;

            this.m_result = GrayscaleImage.Min(this.m_ObrazMinErozja , this.m_DylatacjaMinObraz) * 3.0f;
            this.pbOut.Image = (this.m_result).Clamp().toBitmap();
            this.toolStripProgressBar1.Value = 7;
        }

        private System.Drawing.Bitmap m_source;
        private libmorph.GrayscaleImage m_sourceGray;
        private libmorph.GrayscaleImage m_blur;
        private libmorph.Mask m_blurMask;
        private libmorph.Mask m_operationMask;
        private libmorph.GrayscaleImage m_dilated;
        private libmorph.GrayscaleImage m_eroded;
        private libmorph.GrayscaleImage m_DylatacjaMinObraz;
        private libmorph.GrayscaleImage m_ObrazMinErozja;
        private libmorph.GrayscaleImage m_result;

        private void button1_Click(object sender, EventArgs e)
        {
            this.OnUpdate();
        }
    }
}
